<?php namespace Admin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 分类管理
 */
class CategoryController extends AdminBaseController
{
	//定义数据表
	private $db;

	//定义category表数据
	private $cateData;

	//构造函数，实例化category表，并获得整张表的数据
	public function __construct(){
		parent::__construct();
		$this->db=D('Category');
		$this->cateData=$this->db->getAllData();
	}


	//分类列表
	public function index(){
		$this->assign('data',$this->cateData);
		$this->display();
	}

	//添加分类
	public function add(){
		if (IS_POST) {
			if(!$this->db->store()) $this->error($this->db->getError());
			$this->success('添加成功','index');die;
		}
		$cid=I('get.cid',0,'intval');
		if($cid) $this->assign('cid',$cid);
		$this->assign('data',$this->cateData);
		$this->display();
	}

	//修改分类
	public function edit(){
		$cid=I('get.cid',0,'intval');
		$onedata=$this->db->getDataByCid($cid);
		$data=$this->cateData;
		$childs=$this->db->getChildData($cid);
		foreach ($data as $k => $v) {
			if (in_array($v['cid'],$childs)) {
				$data[$k]['_html']=" disabled='disabled' style='background:#F0F0F0'";
			}else{
				$data[$k]['_html']='';
			}
		}
		$this->assign('data',$data);
		$this->assign('onedata',$onedata);
		$this->display();
	}

	//删除分类
	public function delete(){
		if(!$this->db->deleteData()) $this->error($this->db->getError());
		$this->success('删除成功');
	}

	//排序
	public function sort(){
		$data=I('post.');
		if (!empty($data)) {
			foreach ($data as $k => $v) {
				$this->db->where("cid=$k")->save(['sort'=>$v]);
			}
		}
		$this->success('排序成功');
	}


}